// pages/myExamStack/myExamStack.js
var util = require('../../utils/util.js');
const config = require('../../utils/config');
Page({

  /**
   * 页面的初始数据
   */
  data: {

    examLists: [], //最新考试列表

    examModel: [],  //模拟考试

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    
    

  },

  myExamLists: function(){
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: config.examUrl + '/exam-list1',
      data:{
        userId: 2
      },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        console.log( res.data)
        var examLists = res.data.examListToStart;
        var examModel = res.data.modelTestToStart;
        if(examLists != null){
          for(var i=0; i<examLists.length; i++){
            if(examLists[i].effTime != null){
              examLists[i].effTime = util.formatTimeTwo(examLists[i].effTime,'Y-M-D h:m:s'); 
            }
            if(examLists[i].expTime != null){
              examLists[i].expTime = util.formatTimeTwo(examLists[i].expTime,'Y-M-D h:m:s'); 
            }
          }
          that.setData({
            examLists:  examLists,
          })
          wx.hideLoading();
        } 
        if(examModel != null){
          for(var i=0; i<examModel.length; i++){
            if(examModel[i].effTime != null){
              examModel[i].effTime = util.formatTimeTwo(examModel[i].effTime,'Y-M-D h:m:s'); 
            }
            if(examModel[i].expTime != null){
              examModel[i].expTime = util.formatTimeTwo(examModel[i].expTime,'Y-M-D h:m:s'); 
            }
          }
          that.setData({
            examModel:  examModel,
          })
          wx.hideLoading();
        } 
      },
      fail(){
        wx.hideLoading();
        wx.showToast({
          title: '系统错误',
          image: '/images/erron.png',
          duration: 2000,
        })
      },complete(){
        wx.hideLoading();
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  // 判断是否授权
  var that = this;
  if (wx.getStorageSync('openId') != '') {
    if (wx.getStorageSync('stuCode') == '' || wx.getStorageSync('stuCode') == null) {
            wx.showToast({
              title: '你已授过权, 请登录',
            })
            wx.redirectTo({
              url: '../../pages/login/login?openId=' + wx.getStorageSync('openId')
            })
          } else {
            
            that.myExamLists();
          }
        } else {
          wx.showModal({
            title: '提示',
            content: '你还未授权登录，是否授权登录',
            cancelText: '取消',
            cancelColor: 'red',
            confirmText: '确认',
            confirmColor: '#30a1db',
            success: function (res) {
              //点击取消
              if (res.cancel) {
                  wx.switchTab({
                    url: '../../pages/index/index',
                  })
              } else {
                //点击确定
                wx.redirectTo({
                  url: '../../pages/userlogin/login'
                })
              }
  
            },
            fail: function () {
            }
          })
        }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


})